<?php

// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------

namespace app\portal\controller;

use app\portal\model\PortalProductClassModel;
use app\portal\service\ProductService;
use app\portal\model\PortalProductModel;
use think\facade\Db;
use think\View;

class ProductController extends HomeBaseController {

    // protected $view_share_var = array(
    //     PRODCUT_CATEGORY_LIST => array(
    //         'position' => false, 'type' => 3, 'isSelect' => true, 'controller' => 'product'
    //     ),
    //     NEWS_CATEGORY_LIST => array(//新闻分类列表,对应控制器NewsController
    //         'position' => false, 'type' => 3, 'isSelect' => true, 'controller' => 'news'
    //     ),

    //     CASE_CATEGORY_LIST => array(
    //         'position' => false, 'type' => 2, 'isSelect' => true, 'controller' => 'case'
    //     ),
    //     PRODCUT_RECOMMEND_LIST => array(
    //         'position' => false, 'type' => 2, 'isSelect' => false, 'controller' => 'product'
    //     ),
    //     NEWS_RECOMMEND_LIST => array( 
    //         'position' => false, 'type' => 2, 'isSelect' => false, 'controller' => 'news'
    //     ),
    //     CASE_RECOMMEND_LIST => array( 
    //         'position' => false, 'type' => 2, 'isSelect' => false, 'controller' => 'case'
    //     ),
    // );
    public $view_share_arr = array(
        PRODCUT_CATEGORY_LIST => array(
            'where' => array('post.post_status' => '1', 'post.delete_time' => '0'),
            'order' => 'post.recommended desc,post.list_order',
            'limit' => '8',
            'field' => 'post.id,post.more,post.post_title,post.published_time,post.post_excerpt',
            'table_pre' => 'product'
        ),
        PRODCUT_RECOMMEND_LIST => array(
            'where' => array('post.post_status' => '1', 'post.delete_time' => '0', 'post.post_type' => '1'),
            'order' => 'post.recommended desc,post.list_order ',
            'limit' => '12',
            'field' => 'post.id,post.more,post.post_title,post.published_time,post.post_excerpt',
            'table_pre' => 'product'
        ),
    );

    public function _initialize() {

        parent::_initialize();


    }

    public function index() {
        $id = $this->request->param('id', 0, 'intval');

       // dump($id);die();

        $request_data = $this->request->param();

        $portalCategoryModel = new PortalProductClassModel();

        $category = $portalCategoryModel->where('id', $id)->where('status', 1)->find();

        //dump($category);die();


        if(empty($category)){
            //$category=db('nav_menu')->where('href','/product')->find();
            $category['id'] = 0; 
            $category['name'] ="产品中心";
        }


        $this->assign('category', $category);
        $this->assign('request_data', $request_data);


         $listTpl = empty($category['list_tpl']) ? 'product' : $category['list_tpl'];

        return $this->fetch('product/' . $listTpl);
    }

    public function productDetail() {

        $portalCategoryModel = new PortalProductClassModel();
        $postService = new ProductService();

        $articleId = $this->request->param('id', 0, 'intval');
        $categoryId = $this->request->param('cid', 0, 'intval');
        $article = $postService->publishedArticle($articleId, $categoryId)->toArray();
   
        if(empty($article['more']['photos'])){                        
            $article['more']['photos'] = array();
        }
        
        $article['more']['photos'][] = array(
                'url'=>$article['more']['thumbnail']
            );

        if (empty($article)) {
            abort(404, '产品不存在!');
        }

        $categoryId = $article['category_id'];
        $prevArticle = $postService->publishedPrevArticle($articleId, $categoryId);
        $nextArticle = $postService->publishedNextArticle($articleId, $categoryId);


       
        $category = $portalCategoryModel->where('id', $categoryId)->where('status', 1)->find();

        if (empty($category)) {
            abort(404, '文章不存在!');
        }

        $this->assign('category', $category);

    
        
        
        

        $this->assign('article', $article);
        $this->assign('prev_article', $prevArticle);
        $this->assign('next_article', $nextArticle);


        $oneTpl = empty($category['one_tpl']) ? 'product_detail' : $category['one_tpl'];
        
        return $this->fetch('product/'.$oneTpl);
    }

    

}
